package com.example.product.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.product.entity.Goods;
import com.example.product.entity.Orders;
import com.example.product.exception.CustomException;
import com.example.product.mapper.OrdersMapper;
import com.example.product.service.GoodsService;
import com.example.product.service.OrdersService;
import com.example.product.util.TokenUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

/**
 * 管理员业务处理
 **/
@Service
@Transactional
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private TokenUtil tokenUtil;

    @Autowired
    private OrdersMapper ordersMapper;

    @Resource
    private GoodsService goodsService;

    @Override
    /**
     * 新增
     */
    @Transactional
    public void add(Orders orders) {
        orders.setOrderNo(IdUtil.fastSimpleUUID());  //唯一的订单编号
        orders.setTime(DateUtil.now());
        orders.setStatus("待支付");

        // 扣减库存
        Goods goods = goodsService.selectById(orders.getGoodsId());
        if (goods == null) {
            throw  new CustomException("商品不存在");
        }
        int store = goods.getStore() - orders.getNum();
        if (store < 0) {
            throw  new CustomException("商品库存不足");
        }
        goods.setStore(store);
        goodsService.updateById(goods);
        ordersMapper.insert(orders);
    }

    /**
     * 删除
     */
    @Override
    public void deleteById(Integer id) {
        ordersMapper.deleteById(id);
    }

    @Override
    public boolean saveBatch(Collection<Orders> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<Orders> entityList, int batchSize) {
        return false;
    }

    /**
     * 修改
     *
     * @return
     */
    @Override
    public boolean updateById(Orders orders) {
        if("已取消".equals(orders.getStatus())){ //用户取消订单 要返还库存
            Integer goodsId = orders.getGoodsId();
            Goods goods = goodsService.selectById(goodsId);
            if (goods != null){
                goods.setStore(goods.getStore() + orders.getNum());
                goodsService.updateById(goods);
            }
        }
        ordersMapper.updateById(orders);
        return false;
    }

    @Override
    public boolean updateBatchById(Collection<Orders> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdate(Orders entity) {
        return false;
    }

    @Override
    public Orders getOne(Wrapper<Orders> queryWrapper, boolean throwEx) {
        return null;
    }

    @Override
    public Optional<Orders> getOneOpt(Wrapper<Orders> queryWrapper, boolean throwEx) {
        return Optional.empty();
    }

    @Override
    public Map<String, Object> getMap(Wrapper<Orders> queryWrapper) {
        return null;
    }

    @Override
    public <V> V getObj(Wrapper<Orders> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }

    @Override
    public OrdersMapper getBaseMapper() {
        return null;
    }

    @Override
    public Class<Orders> getEntityClass() {
        return null;
    }

    /**
     * 根据ID查询
     */
    @Override
    public Orders selectById(Integer id) {
        return ordersMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    @Override
    public List<Orders> selectAll(Orders orders) {
        return ordersMapper.selectAll(orders);
    }

    /**
     * 分页查询
     */
    @Override
    public PageInfo<Orders> selectPage(Orders orders, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Orders> list = ordersMapper.selectAll(orders);
        return PageInfo.of(list);
    }

}